# 13_jes2001_2016_ideology_political_psychology.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-13 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("dplyr")
require("tidyr")
require("ggplot2")
require("ggpubr")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# function to calculate SE
se <- function(x) {
  return(sd(x, na.rm = TRUE) / sqrt(length(x[!is.na(x)])))
}


# 2009
## merge
JES2009_Ideology <- JES2009_Respondent[,c("ID", "Ideology")]
JES2009_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave3.sav", 
  user_na = FALSE
)
JES2009_Ideology <- left_join(
  JES2009_Ideology, 
  JES2009_2[,c("ID", "Q14_1", "Q14_2", "Q14_3", "Q15_1")], 
  by = "ID"
)
JES2009_Ideology <- left_join(
  JES2009_Ideology, 
  JES2009[,c("ID", "Q32", "Q37_1")], 
  by = "ID"
)
colnames(JES2009_Ideology) <- c("ID", "Ideology", "Trust1", "Trust2", "Trust3", 
                                "Meaningless", "Interest", "Efficacy")
JES2009_Ideology <- as.data.frame(JES2009_Ideology)

## recode
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                            JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                            JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                            JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2009_Ideology$Position[JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
JES2009_Ideology$Trust1[JES2009_Ideology$Trust1 %in% c(8, 9)] <- NA
JES2009_Ideology$Trust2[JES2009_Ideology$Trust2 %in% c(8, 9)] <- NA
JES2009_Ideology$Trust3[JES2009_Ideology$Trust3 %in% c(8, 9)] <- NA
JES2009_Ideology$Trust <- rowMeans(
  JES2009_Ideology[,c("Trust1", "Trust2", "Trust3")], na.rm = TRUE
)
JES2009_Ideology$Interest[JES2009_Ideology$Interest %in% c(8, 9)] <- NA
JES2009_Ideology$Efficacy[JES2009_Ideology$Efficacy %in% c(8, 9)] <- NA
JES2009_Ideology$Meaningless[JES2009_Ideology$Meaningless %in% c(8, 9)] <- NA


# 2013
JES2013_Ideology <- JES2013_Respondent[,c("id", "Ideology")]
JES2013_Ideology <- left_join(
  JES2013_Ideology, 
  JES2013[,c("id", "b11_1", "b11_2", "b11_3", "b12_1", "a31", "a36x1")], 
  by = "id"
)
colnames(JES2013_Ideology) <- c("id", "Ideology", "Trust1", "Trust2", "Trust3", 
                                "Meaningless", "Interest", "Efficacy")
JES2013_Ideology <- as.data.frame(JES2013_Ideology)

## recode
JES2013_Ideology$Position[JES2013_Ideology$Ideology > quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2013_Ideology$Position[JES2013_Ideology$Ideology > quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                            JES2013_Ideology$Ideology <= quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2013_Ideology$Position[JES2013_Ideology$Ideology > quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                            JES2013_Ideology$Ideology <= quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2013_Ideology$Position[JES2013_Ideology$Ideology > quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                            JES2013_Ideology$Ideology <= quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2013_Ideology$Position[JES2013_Ideology$Ideology <= quantile(JES2013_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
JES2013_Ideology$Trust <- rowMeans(
  JES2013_Ideology[,c("Trust1", "Trust2", "Trust3")], na.rm = TRUE
)


# plot
## political trust
Trust_Ideology <- data.frame(
  Year = c(rep(2009, nrow(JES2009_Ideology)), rep(2013, nrow(JES2013_Ideology))), 
  Position = c(JES2009_Ideology$Position, JES2013_Ideology$Position), 
  Trust = c(JES2009_Ideology$Trust, JES2013_Ideology$Trust)
)
Trust_Ideology$Year <- as.factor(Trust_Ideology$Year)
Trust_Ideology <- na.omit(Trust_Ideology)
Trust_Ideology_Summary <- aggregate(
  Trust ~ Year + Position, Trust_Ideology, FUN = mean
)
colnames(Trust_Ideology_Summary)[3] <- "Mean"
Trust_Ideology_Summary$SE <- aggregate(
  Trust ~ Year + Position, Trust_Ideology, FUN = se
)[,3]
Trust_Ideology_plot <- ggplot(
  Trust_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Low  %<-%  "Political Distrust" %->% High), 
       title = "(a) Political Distrust", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Trust_Ideology_plot)

## political interest
Interest_Ideology <- data.frame(
  Year = c(rep(2009, nrow(JES2009_Ideology)), rep(2013, nrow(JES2013_Ideology))), 
  Position = c(JES2009_Ideology$Position, JES2013_Ideology$Position), 
  Interest = c(JES2009_Ideology$Interest, JES2013_Ideology$Interest)
)
Interest_Ideology$Year <- as.factor(Interest_Ideology$Year)
Interest_Ideology <- na.omit(Interest_Ideology)
Interest_Ideology_Summary <- aggregate(
  Interest ~ Year + Position, Interest_Ideology, FUN = mean
)
colnames(Interest_Ideology_Summary)[3] <- "Mean"
Interest_Ideology_Summary$SE <- aggregate(
  Interest ~ Year + Position, Interest_Ideology, FUN = se
)[,3]
Interest_Ideology_plot <- ggplot(
  Interest_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Low  %<-%  "Indifference" %->% High), 
       title = "(b) Indifference to Politics", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Interest_Ideology_plot)

## political efficacy
Efficacy_Ideology <- data.frame(
  Year = c(rep(2009, nrow(JES2009_Ideology)), rep(2013, nrow(JES2013_Ideology))), 
  Position = c(JES2009_Ideology$Position, JES2013_Ideology$Position), 
  Efficacy = c(JES2009_Ideology$Efficacy, JES2013_Ideology$Efficacy)
)
Efficacy_Ideology$Year <- as.factor(Efficacy_Ideology$Year)
Efficacy_Ideology <- na.omit(Efficacy_Ideology)
Efficacy_Ideology_Summary <- aggregate(
  Efficacy ~ Year + Position, Efficacy_Ideology, FUN = mean
)
colnames(Efficacy_Ideology_Summary)[3] <- "Mean"
Efficacy_Ideology_Summary$SE <- aggregate(
  Efficacy ~ Year + Position, Efficacy_Ideology, FUN = se
)[,3]
Efficacy_Ideology_plot <- ggplot(
  Efficacy_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Low  %<-%  "External Political Efficacy" %->% High), 
       title = "(c) External Political Efficacy", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Efficacy_Ideology_plot)

## significance of vote
Meaningless_Ideology <- data.frame(
  Year = c(rep(2009, nrow(JES2009_Ideology)), rep(2013, nrow(JES2013_Ideology))), 
  Position = c(JES2009_Ideology$Position, JES2013_Ideology$Position), 
  Meaningless = c(JES2009_Ideology$Meaningless, JES2013_Ideology$Meaningless)
)
Meaningless_Ideology$Year <- as.factor(Meaningless_Ideology$Year)
Meaningless_Ideology <- na.omit(Meaningless_Ideology)
Meaningless_Ideology_Summary <- aggregate(
  Meaningless ~ Year + Position, Meaningless_Ideology, FUN = mean
)
colnames(Meaningless_Ideology_Summary)[3] <- "Mean"
Meaningless_Ideology_Summary$SE <- aggregate(
  Meaningless ~ Year + Position, Meaningless_Ideology, FUN = se
)[,3]
Meaningless_Ideology_plot <- ggplot(
  Meaningless_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Less  %<-%  "Significance of My Vote" %->% High), 
       title = "(d) Significance of My Vote", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Meaningless_Ideology_plot)
